<template>
  <div class="singerBox" @scroll="freshData">
    <div class="singer">
      <div>
        <singer-cate ref="cate" @changeTag="getSingerList"></singer-cate>
      </div>
      <div class="singerList">
        <div v-for="item in singerList" :key="item.id">
          <img :src="item.img1v1Url" loading="lazy" alt width="151" height="151" @click="goSingerInfo(item.id)" />
          <div class="singerName">
            <span>{{item.name}}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import singerCate from './singerCate.vue'
// import LazyImg from '@/components/lazyImg'
import axios from '@/http'
import { onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'
onMounted(() => {
  getSingerList()
})
onUnmounted(() => {
  timer.value && clearTimeout(timer.value)
})
const router = useRouter()
const cate = ref(null)
const singerList = ref([])
const more = ref<boolean>(false)
const offset = ref<number>(0)
const timer = ref(null)
const goSingerInfo = id => {
  router.push('/home/singerInfo/' + id)
}
const freshData = e => {
  const { scrollTop, scrollHeight } = e.target
  if (timer.value) {
    clearTimeout(timer.value)
  }
  timer.value = setTimeout(() => {
    if (scrollTop + 650 > scrollHeight) {
      more.value && addSingerList()
    }
  }, 200)
}
const getSingerList = () => {
  offset.value = 0
  axios
    .get('/artist/list', {
      params: {
        type: cate.value.currentCate,
        area: cate.value.currentLang,
        initial: cate.value.currentEn,
        offset: offset.value
      }
    })
    .then(res => {
      singerList.value = [...res.data.artists]
      more.value = res.data.more
      offset.value = 30
    })
}
const addSingerList = () => {
  axios
    .get('/artist/list', {
      params: {
        type: cate.value.currentCate,
        area: cate.value.currentLang,
        initial: cate.value.currentEn,
        offset: offset.value
      }
    })
    .then(res => {
      singerList.value = [...singerList.value, ...res.data.artists]
      more.value = res.data.more
      offset.value += 30
    })
}
</script>

<style scoped lang="scss">
.singerBox {
  width: 100%;
  height: 620px;
  overflow: scroll;
  .singer {
    padding: 6px 48px 50px;
    box-sizing: border-box;
    .singerList {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      div {
        flex-basis: 151px;

        img {
          border-radius: 8px;
          &:hover {
            cursor: pointer;
          }
        }
        .singerName {
          margin-bottom: 30px;
          color: #b3b3b3;
          font-size: 13px;
          font-weight: bold;
          span {
            &:hover {
              color: #fff;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
}
</style>